<?php 
## include required files
/*******************************/
require_once '../model/state.php';
/*******************************/
## Create Objects
/*******************************/
$countryObj = new Model_State();
$stateObj = new Model_State();
/*******************************/

if(isset($_POST['addstate_btn'])) {	
	## apply PHP validation for required filed
	
	if(trim($_POST['statename']) != '' && trim($_POST['siso_code']) != '') {
		
		$getdetailstate = $stateObj->checkStateName($_POST['statename']);
		if($getdetailstate['state_id']=='')
		{
			$stateAddArray['country_id']   = $_POST['country'];		
			$stateAddArray['iso1_code']      = $_POST['siso_code'];
			$stateAddArray['state_name']      = $_POST['statename'];
			$stateAddArray['state_status']      = '1';
			$stateObj->addState($stateAddArray);
			$_SESSION['msg'] = "<div class='success_msg'><span>State added successfully</span></div>";
			header('location:'.SITE_URL.'/admin/home.php?q=state&page='.$_POST['page']);			
			exit;
		}
		else
		{// php validation else
			$_SESSION['msg'] = '<div class="error_msg"><span>State name already used.</span></div>';
		}
	} else {// php validation else
		$_SESSION['msg'] = '<div class="error_msg"><span>Please fill all required fields</span></div>';
	}
	// main if closed
}

## Fetch all active countries
$countryArray = $countryObj->getAllCountryWithActiveStatus();
$smarty->assign('countryArray', $countryArray);


## Assign success or error msg to smarty variable and unset session variable
if(trim($_SESSION['msg']) != '') {
	$smarty->assign('msg', $_SESSION['msg']);
	unset($_SESSION['msg']);
}

## Set active class variable for left menu
$smarty->assign('activeclass', 'state');
$smarty->assign('mainmenu', '2');
## Unset all the objects created which are on this page
unset($countryObj);
unset($stateObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'state/addstate.tpl');
unset($smarty);
?>